package com.ywjg.serviceimp;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.ywjg.dao.AppGoodDao;
import com.ywjg.dao.GoodTypeDao;
import com.ywjg.model.AppGood;
import com.ywjg.model.GoodType;
import com.ywjg.service.IGoodTypeService;
import com.ywjg.utils.ApplicationModel;

@Service
public class GoodTypeService implements IGoodTypeService  {

	@Resource
	GoodTypeDao goodTypeDao;
	@Resource
	AppGoodDao appGoodDao;
	@Autowired
	ApplicationModel applicationModel;

	/* (non-Javadoc)
	 * @see com.ywjg.serviceimp.IGoodTypeService#addGoodType(com.ywjg.model.GoodType)
	 */
	public GoodType addGoodType(GoodType goodType) {
		GoodType save = goodTypeDao.save(goodType);
		return save;
	}

	/* (non-Javadoc)
	 * @see com.ywjg.serviceimp.IGoodTypeService#deleteOneGoodType(java.lang.Integer)
	 */
	public Boolean deleteOneGoodType(Integer id) {
		List<GoodType> goodTypes = goodTypeDao.getGoodTypeByParentId(id);
		if (null != goodTypes && goodTypes.size() != 0) {
			goodTypeDao.delete(id);
			return true;
		} else {
			return false;
		}
	}

	/* (non-Javadoc)
	 * @see com.ywjg.serviceimp.IGoodTypeService#getGoodTypeByParentId(java.lang.Integer)
	 */
	public List<GoodType> getGoodTypeByParentId(Integer id) {
		// TODO Auto-generated method stub
		return goodTypeDao.getGoodTypeByParentId(id);
	}

	/* (non-Javadoc)
	 * @see com.ywjg.serviceimp.IGoodTypeService#getThisTypeGoods(java.lang.Integer, java.lang.Integer, java.lang.Integer)
	 */
	public List<AppGood> getThisTypeGoods(Integer id,Integer page,Integer size) {
		// TODO Auto-generated method stub
		Pageable Pageable = new PageRequest(page, size);
		Page<AppGood> thisTypeGoods = appGoodDao.findAppGoodByGoodTypeId(id, Pageable);
		int numberOfElements = (int) thisTypeGoods.getTotalElements();
		applicationModel.getMap().put("getThisTypeGoods", numberOfElements);
		return thisTypeGoods.getContent();
	}
}
